/*
 * Copyright (c) 2010-2013 Ruben Chakhmakhchyan. All rights reserved.
 * 
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Eclipse Public License
 * Version 1.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://opensource.org/licenses/EPL-1.0 and read it before using this file.
 */

#include "commontwain.h"


TW_FIX32 floatToFIX32( float floater )
{
	TW_FIX32 Fix32_value;
	bool sign = floater < 0;
	TW_INT32 value = ( TW_INT32 ) ( floater * 65536.0f + ( sign ? -0.5f : 0.5f ) );
	Fix32_value.Whole = ( TW_INT16 ) floater;//;( TW_INT16 )( value >> 16 );
	Fix32_value.Frac = ( TW_UINT16 )( value & 0x0000ffffL );
	return ( Fix32_value );
}

TW_FIX32 floatToFIX32( double floater )
{
	TW_FIX32 Fix32_value;
	bool sign = floater < 0;
	TW_INT32 value = ( TW_INT32 ) ( floater * 65536.0f + ( sign ? -0.5f : 0.5f ) );
	Fix32_value.Whole = ( TW_INT16 ) floater;//;( TW_INT16 )( value >> 16 );
	Fix32_value.Frac = ( TW_UINT16 )( value & 0x0000ffffL );
	return ( Fix32_value );
}

float fix32ToFloat( const TW_FIX32& _fix32 )
{
	return float( _fix32.Whole ) + float( _fix32.Frac / 65536.0f );
}

bool operator== ( const TW_FIX32& _fix1, const TW_FIX32& _fix2 )
{
	return ( _fix1.Whole == _fix2.Whole ) && ( _fix1.Frac == _fix2.Frac );
}

int getTWTYsize( TW_UINT16 itemType )
{
#define CASEITEMTYPE( itemType ) case TWTY_##itemType: typeSize = sizeof( TW_##itemType ); break

	int typeSize = 0;

	switch( itemType )
	{
		CASEITEMTYPE( INT8 );
		CASEITEMTYPE( INT16 );
		CASEITEMTYPE( INT32 );
		CASEITEMTYPE( UINT8 );
		CASEITEMTYPE( UINT16 );
		CASEITEMTYPE( UINT32 );
		CASEITEMTYPE( BOOL );
		CASEITEMTYPE( FIX32 );
		CASEITEMTYPE( FRAME );
		CASEITEMTYPE( STR32 );
		CASEITEMTYPE( STR64 );
		CASEITEMTYPE( STR128 );
		CASEITEMTYPE( STR255 );
		//CASEITEMTYPE(  STR1024  );
		//CASEITEMTYPE(  UNI512 );
		//CASEITEMTYPE(  HANDLE  );
	default:
		break;
	}
	return typeSize;

#undef CASEITEMTYPE
}
